﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using IdentityServer4.EntityFramework.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace AuthenticationServer.Data
{
    //public class ConfigurationDbDesignTimeDbContextFactory
    //{
    //}
    //public class ConfigurationDbDesignTimeDbContextFactory : DesignTimeDbContextFactory<IdentityServerConfigurationDbContext>
    //{
    //}
    public class ConfigurationDbDesignTimeDbContextFactory : IDesignTimeDbContextFactory<IdentityServerConfigurationDbContext>
    {
        public IdentityServerConfigurationDbContext CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            var builder = new DbContextOptionsBuilder<IdentityServerConfigurationDbContext>();

            var connectionString = configuration.GetConnectionString("DefaultConnection");
            var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

            builder.UseSqlServer(connectionString);

            return new IdentityServerConfigurationDbContext(builder.Options,
                new ConfigurationStoreOptions()
                {
                    ApiClaim = new TableConfiguration(IdentityServerTableNames.ApiClaims),
                    ApiResource = new TableConfiguration(IdentityServerTableNames.ApiResources),
                    ApiScope = new TableConfiguration(IdentityServerTableNames.ApiScopes),
                    ApiScopeClaim = new TableConfiguration(IdentityServerTableNames.ApiScopeClaims),
                    ApiSecret = new TableConfiguration(IdentityServerTableNames.ApiSecrets),

                    Client = new TableConfiguration(IdentityServerTableNames.Clients),
                    ClientClaim  = new TableConfiguration(IdentityServerTableNames.ClientClaims),
                    ClientCorsOrigin = new TableConfiguration(IdentityServerTableNames.ClientCorsOrigins),
                    ClientGrantType = new TableConfiguration(IdentityServerTableNames.ClientGrantTypes),
                    ClientIdPRestriction = new TableConfiguration(IdentityServerTableNames.ClientIdPRestrictions),
                    ClientPostLogoutRedirectUri = new TableConfiguration(IdentityServerTableNames.ClientPostLogoutRedirectUris),
                    ClientProperty = new TableConfiguration(IdentityServerTableNames.ClientProperties),
                    ClientRedirectUri = new TableConfiguration(IdentityServerTableNames.ClientRedirectUris),
                    ClientScopes = new TableConfiguration(IdentityServerTableNames.ClientScopes),
                    ClientSecret = new TableConfiguration(IdentityServerTableNames.ClientSecrets),

                    IdentityClaim = new TableConfiguration(IdentityServerTableNames.IdentityClaims),
                    IdentityResource = new TableConfiguration(IdentityServerTableNames.IdentityResources),

                    ConfigureDbContext = b =>
                      b.UseSqlServer(connectionString,
                          sql => sql.MigrationsAssembly(migrationsAssembly))
                    //ConfigureDbContext = o => { o. }

                });
        }
    }
}
